package cn.tedu.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class Server {
    private ServerSocket serverSocket;
//    private PrintWriter[] allOut = {};
    private Collection<PrintWriter> allOut = new ArrayList<>();

    public Server() {
        try {
            System.out.println("正在启动服务器端");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器端启动完毕");
        } catch (IOException e) {
            System.out.println("服务器端构造器处理IO异常");
        }
    }
    public void start(){
        try {
            System.out.println("等待客户端连接");
            Socket socket = serverSocket.accept();
            System.out.println("一个客户端连接了");

            ClientHandler clientHandler = new ClientHandler(socket);
            Thread t = new Thread(clientHandler);
            t.start();
        } catch (IOException e) {
            System.out.println("accept()方法抛出异常");
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;
        public ClientHandler(Socket socket){
            this.socket = socket;
             host = socket.getInetAddress().getHostAddress();
        }

        @Override
        public void run() {
            PrintWriter pw = null;
            try {

                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in,"UTF-8");
                BufferedReader br = new BufferedReader(isr);

                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);

                synchronized (Server.this){
//                    allOut = Arrays.copyOf(allOut, allOut.length + 1);
//                    allOut[allOut.length-1] = pw;
                    allOut.add(pw);
                }
//                sendMessage(host+"上线了,当前在线人数:"+allOut.length);
                sendMessage(host+"上线了,当前在线人数:"+allOut.size());

                String line = null;
                while ((line = br.readLine())!=null){
                    System.out.println(host+"客户端说:"+line);
//                    for (int i=0;i<allOut.length;i++){
//                       allOut[i].println(host+"说:"+ line);
                       sendMessage(host+"说:"+line);
//                    }
                }
            } catch (IOException e) {
                System.out.println("start()方法抛出异常");
            }finally {
                synchronized (Server.this){
//                    for (int i=0;i<allOut.length;i++){
//                        if (allOut[i] == pw){
//                            allOut[i] = allOut[allOut.length-1];
//                            allOut = Arrays.copyOf(allOut,allOut.length-1);
//                            break;
//                        }
//                    }
                    allOut.remove(pw);
                }

//                sendMessage(host+"下线了，当前在线人数:"+allOut.length);
                sendMessage(host+"下线了，当前在线人数:"+allOut.size());
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void sendMessage(String message) {
        synchronized (Server.this){
//            for (int i=0;i< allOut.length;i++){
//                allOut[i].println(message);
//            }
            for (PrintWriter pw : allOut) {
                pw.println(message);
            }
        }

    }
}
